package top.jle.jpa.basic.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import top.jle.jpa.basic.dao.PageRequest;
import top.jle.jpa.basic.dao.SortKey;

import java.util.Optional;


/**
 * @author jim.lee
 */
public abstract class BaseController {

    @Resource
    private HttpServletRequest request;
    @Resource
    private HttpServletResponse response;


    protected String getBasePath() {
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
    }

    public String getRequestUrl() {
        return request.getRequestURL().toString();
    }


    public String getFullRequestUrl() {
        StringBuilder url = new StringBuilder(request.getRequestURL());
        if (request.getQueryString() != null) {
            url.append('?');
            url.append(request.getQueryString());
        }
        return url.toString();
    }

    protected String getParameterValue(String parameter) {
        String param = request.getParameter(parameter);
        if (StringUtils.isNotBlank(param)) {
            return param;
        } else {
            return Optional.ofNullable(request.getAttribute(parameter)).orElse("").toString();
        }
    }


    public PageRequest getPageRequestData(int page,int rows) {
        page = Math.max(page, 1);
        if (rows<1) {
            rows=1;
        }
        int offset = (page - 1) * rows;
        PageRequest pageRequest = new PageRequest();
        pageRequest.setOffset(offset);
        pageRequest.setLimit(rows);
        pageRequest.setCurrentPage(page);

        return pageRequest;
    }

    public PageRequest getPageRequestData() {
        String ascend = getParameterValue("order");
        String sortProperty = getParameterValue("sort");
        String page = getParameterValue("page");
        String limit = getParameterValue("limit");
        int intPage = 1;
        if (StringUtils.isNumeric(page)) {
            intPage = Integer.parseInt(page);
        }
        int rowsPerPage = 20;
        if (StringUtils.isNumeric(limit)) {
            rowsPerPage = Integer.parseInt(limit);
        }
        intPage = Math.max(intPage, 1);
        if (rowsPerPage<1) {
            rowsPerPage=1;
        }
        int offset = (intPage - 1) * rowsPerPage;
        PageRequest pageRequest = new PageRequest();
        pageRequest.setOffset(offset);
        pageRequest.setLimit(rowsPerPage);
        pageRequest.setCurrentPage(intPage);
        if (StringUtils.isNotBlank(sortProperty) && ascend != null) {
            pageRequest.setOrderBy(new SortKey[]{"asc".equals(ascend) ? SortKey.asc(sortProperty) : SortKey.desc(sortProperty)});
        }
        return pageRequest;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }



}
